/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.incad.psh.pshassign.server.search;

import com.google.gwt.regexp.shared.MatchResult;
import com.google.gwt.regexp.shared.RegExp;
import cz.incad.psh.pshassign.server.Options;
import java.io.InputStream;
import java.io.StringWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;

/**
 *
 * @author alberto
 */
public class Synonyms {
    public static boolean contains(String text, String word) {
        try {
            String urlStr = Options.getOption("psh", "solr") + "/select?debugQuery=true&wt=json&rows=0&q=syn:" + 
                    URLEncoder.encode(word.replaceAll(" ", "").replaceAll("-", ""), "UTF-8");
            
            java.net.URL url = new java.net.URL(urlStr);
            
            InputStream is = url.openStream();
            StringWriter writer = new StringWriter();
            IOUtils.copy(is, writer, "UTF-8");
            JSONObject js = new JSONObject(writer.toString());
            String parsedquery_toString = js.getJSONObject("debug").getString("parsedquery_toString");
            String[] syns = parsedquery_toString.replaceAll("syn\\:", "").split(" ");
            for(String syn : syns){
                if(text.contains(syn)){
                    return true;
                }
            }
            return false;
            
        } catch (Exception e) {
            System.out.println("error :\n" + e);
            e.printStackTrace();
            return false;
        }
    }
    
    public static int getNumHits(String text, ArrayList<String> syns) {
        int res = 0;
        
        try {
            for(String syn : syns){
                String ptr = "\\b" + syn + "\\b";
                RegExp regExp = RegExp.compile(ptr, "ig");

                for (MatchResult matcher = regExp.exec(text); matcher != null; matcher = regExp.exec(text)) {
                    res++;
                }
            }
            
        } catch (Exception e) {
            System.out.println("error :\n" + e);
            e.printStackTrace();
        }
            return res;
    }
    
    
    public static boolean contains(String text, ArrayList<String> syns) {
        
        try {
            for(String syn : syns){
                if(text.contains(syn)){
                    return true;
                }
            }
            return false;
            
        } catch (Exception e) {
            System.out.println("error :\n" + e);
            e.printStackTrace();
            return false;
        }
    }

    public static ArrayList<String> getSynonyms(String word) {
        
        try {
            String urlStr = Options.getOption("psh", "solr") + "/select?debugQuery=true&wt=json&rows=0&q=syn:" + 
                    URLEncoder.encode(word.replaceAll(" ", "").replaceAll("-", ""), "UTF-8");
            
            java.net.URL url = new java.net.URL(urlStr);
            
            InputStream is = url.openStream();
            StringWriter writer = new StringWriter();
            IOUtils.copy(is, writer, "UTF-8");
            JSONObject js = new JSONObject(writer.toString());
            String parsedquery_toString = js.getJSONObject("debug").getString("parsedquery_toString");
            return new ArrayList<String>(Arrays.asList(parsedquery_toString.replaceAll("syn\\:", "").split(" ")));
            
        } catch (Exception e) {
            System.out.println("error :\n" + e);
            e.printStackTrace();
            return null;
        }
    }
}
